let pager = {}; //分页
pager.pageNum = 1;
let totalPage; //总页码
let total;//总条数
pullRefresh(pager); //启用上拉下拉
let listNum = 0;//总条数


mui('body').on('tap', '.contextDivList', function () {
    let contextDivId = this.id;
    let num = contextDivId.replace("contextDiv", "");
    let msgTxtDiv = '#msgTxtDiv' + num;
    let display = $(msgTxtDiv).css('display')
    if (display == 'none') {
        $(msgTxtDiv).show();
    } else {
        $(msgTxtDiv).hide();
    }
})


//点击列表项图片
mui('body').on('tap', '.contextImgList', function () {
    //默认图片不可点击
    var currImg = $(this);
    let htmlHref = currImg.attr('src');
    htmlHref = htmlHref.replace(/^http:\/\/[^/]+/, "");
    let addr = htmlHref.substr(htmlHref.lastIndexOf('/', htmlHref.lastIndexOf('/') - 1) + 1);
    let index = addr.lastIndexOf("\/");
    //js 获取字符串中最后一个斜杠后面的内容
    let addrLast = decodeURI(addr.substring(index + 1, addr.length));
    if ("default.png" == addrLast) {
        WeKnow.notifyMsg("无处方图片");
        return;
    }

    //放大处方图片
    var tempContainer = $('<div class=tempContainer></div>');//图片容器
    with (tempContainer) {//width方法等同于$(this)
        appendTo("body");
        //获取图片原始宽度、高度
        css('top', 0);
        html('<img  style="height: 100%;width: 100%" border=0 src=' + currImg.attr('src') + '>');
    }
    tempContainer.click(function () {
        $(this).remove();
    });
});


function pullRefresh() {
    mui("#refreshContainer").pullRefresh({
        up: {
            auto: true, //可选,默认false.首次加载自动下拉刷新一次
            contentdown: "下拉可以加载更多", //可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
            contentover: "释放立即加载", //可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
            contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore: '没 有 更 多 数 据 了', //可选，请求完毕若没有更多数据时显示的提醒内容；
            style: 'circle',//必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
            color: '#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
            height: '50px',//可选,默认50px.下拉刷新控件的高度,
            range: '100px', //可选 默认100px,控件可下拉拖拽的范围
            offset: '0px', //可选 默认0px,下拉刷新控件的起始位置
            callback: function () { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
                window.setTimeout(function () {
                    pager.pageSize = 10; //条数
                    getData(pager);
                }, 500);
            }
        },

        down: {
            callback: pulldownRefresh
        }

    })
}

/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
    window.location.reload();
    mui('#refreshContainer').pullRefresh().endPulldownToRefresh(); //refresh completed
}

$(".selectAllOrSelf").change(function () {
    $('#violationListId').empty();
    let pager = {}; //分页
    pager.pageNum = 1;
    totalPage = 0; //总页码
    total = 0;//总条数
    listNum = 0;//总条数
    pullRefresh(pager); //启用上拉下拉
});


function getData(params) {
    let pageNum = params.pageNum;
    let pageSize = params.pageSize;
    console.log(pageNum)
    console.log(pageSize)
    mui.ajax("violationRecord/violationRecordList", {
        data: {
            pageNum: pageNum,
            pageSize: pageSize,
            institutionId: $("#hiddenInstitutionId").text(),
            flg: $('#selectAllOrSelf').val()
        },
        dataType: 'json', //服务器返回json格式数据
        type: 'post', //HTTP请求类型
        timeout: 10000, //超时时间设置为10秒；
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (data) {
            $("#manualAuditNum").text(data.num);
            total = data.total;
            mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
            if (data.errCode === 0) {
                let violationManageModelList = data.violationManageModelList;
                let violationListId = document.getElementById('violationListId');
                for (let i = 0; i < violationManageModelList.length; i++) {
                    listNum++;
                    let prescriptionPicUrl = "img/default.png";
                    if (violationManageModelList[i].prescriptionPicUrl != null) {
                        prescriptionPicUrl = violationManageModelList[i].prescriptionPicUrl;
                    }

                    let html =
                        '<li  class="mui-table-view-cell mui-media" >' +
                        '<a href="javascript:;">' +
                        '<img id="contextImg' + listNum + '" class="mui-media-object mui-pull-right contextImgList" src="' + prescriptionPicUrl + '">' +
                        '<div id="contextDiv' + listNum + '" class="contextDivList">'
                    if (violationManageModelList[i].autoAudit == 1) {
                        html += '<span class="mui-badge mui-badge-green">系统审核</span>'
                    }
                    if (violationManageModelList[i].autoAudit == 2) {
                        html += '<span class="mui-badge mui-badge-blue">人工审核</span>'
                    }
                    html += '<span>' + violationManageModelList[i].title + '</span>'
                    html += '<p class="mui-ellipsis">'
                    html += '<span>' + violationManageModelList[i].uploadTmStr + '</span> '
                    html += '<span>' + violationManageModelList[i].name + '</span>'
                    html += '</p>'
                    html += '<textarea readonly id="msgTxtDiv' + listNum + '" style="display: none" class="msgTxtTextArea">' + violationManageModelList[i].msgTxt + '</textarea>'
                    html += '</div>'
                    html += '</a>'
                    html += '</li>';
                    violationListId.innerHTML += html;
                }

                //这里很重要，这里获取页码 公式：总条数/每页显示条数
                totalPage = total % pager.pageSize != 0 ? parseInt(total / pager.pageSize) + 1 : total / pager.pageSize;


                if (totalPage == pager.pageNum || totalPage == 0) { //总页码等于当前页码，停止上拉下拉
                    mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                } else {
                    pager.pageNum = pager.pageNum + 1;
                    mui('#refreshContainer').pullRefresh().refresh(true);
                }

            } else if (data.result == '0001') {

            }

        },
        error: function (xhr, type, errorThrown) {
            //异常处理；

        }
    })
}



